<!DOCTYPE html>
            
<HTML>
<HEAD>
<meta name="booktitle" content="Developing Applications With Objective Caml" >
 <meta charset="ISO-8859-1"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<META name="GENERATOR" content="hevea 1.05-7 of 2000-02-24">
<META NAME="Author" CONTENT="Christian.Queinnec@lip6.fr">
<LINK rel=stylesheet type="text/css" href="videoc-ocda.css">
<script language="JavaScript" src="videoc.js"><!--
//--></script>
<TITLE>
 Which Style to Choose?
</TITLE>
</HEAD>
<BODY class="regularBody">
<A HREF="book-ora037.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora039.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2> Which Style to Choose?</H2> 
This is no matter of religion or esthetics; a priori
neither style is prettier or holier than the other. On the contrary,
one style may be more adequate than the other depending on the problem
to be solved. <BR>
<BR>
The first rule to apply is the rule of simplicity. Whether the
algorithm to use implemented is written in a book, or whether its seed
is in the mind of the programmer, the algorithm is itself described in
a certain style. It is natural to use the same style when
implementing it.<BR>
<BR>
The second criterion of choice is the efficiency of the program. One
may say that an imperative program (if well written) is more efficient
that its functional analogue, but in very many cases the difference is
not enough to justify complicating the code to adopt an imperative
style where the functional style would be natural. The function
<TT>map</TT> in the previous section is a good example of a problem
naturally expressed in the functional style, which gains nothing from
being written in the imperative style.<BR>
<BR>
<A NAME="toc48"></A>
<H3> Sequence or Composition of Functions</H3>
<A NAME="@concepts110"></A>We have seen that as soon as a program causes side effects, it is
necessary to determine precisely the order of evaluation for the
elements of the program. This can be done in both styles: 
<DL COMPACT=compact>
<DT>
functional: <DD> using the fact that Objective CAML is a strict
language, which means that the argument is evaluated before applying
the function. The expression <TT>(</TT>f<CODE> </CODE><TT>(</TT>g<CODE> </CODE>x<TT>)</TT><TT>)</TT> is computed by
first evaluating <TT>(</TT>g<CODE> </CODE>x<TT>)</TT>, and then passing the result as
argument to <TT>f</TT>. With more complex expressions, we can name an
intermediate result with the <B>let in</B> construction, but the idea
remains the same:
<B>let</B><CODE> </CODE>aux<CODE>=</CODE><TT>(</TT>g<CODE> </CODE>x<TT>)</TT><CODE> </CODE><B>in</B><CODE> </CODE><TT>(</TT>f<CODE> </CODE>aux<TT>)</TT>. 

<DT>imperative: <DD> using sequences or other control structures
(loops). In this case, the result is not the value returned by a
function, but its side effects on memory: 
aux<CODE>:=</CODE><TT>(</TT>g<CODE> </CODE>x<TT>)</TT><CODE> </CODE>;<CODE> </CODE><TT>(</TT>f<CODE> </CODE><CODE>!</CODE>aux<TT>)</TT>. 
</DL>Let us examine this choice of style on an example. The quick
sort algorithm, applied to a vector, is described recursively as
follows: 
<OL type=1>
<LI>
 Choose a pivot: This is the index of an element of the vector; 

<LI> Permute around the pivot: Permute the elements of the vector so
elements less than the value at the pivot have indices less than the
pivot, and vice versa; 

<LI> sort the subvectors obtained on each side of the pivot, using
the same algorithm: The subvector preceding the pivot and the
subvector following the pivot. 
</OL>
The choice of algorithm, namely to modify a vector so that its
elements are sorted, incites us to use an imperative style at least to
manipulate the data. <BR>
<BR>
First, we define a function to permute two elements of a vector:


<PRE><BR># <B>let</B><CODE> </CODE>permute_element<CODE> </CODE>vec<CODE> </CODE>n<CODE> </CODE>p<CODE> </CODE><CODE>=</CODE><CODE> </CODE><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><B>let</B><CODE> </CODE>aux<CODE> </CODE><CODE>=</CODE><CODE> </CODE>vec<CODE>.</CODE><TT>(</TT>n<TT>)</TT><CODE> </CODE><B>in</B><CODE> </CODE>vec<CODE>.</CODE><TT>(</TT>n<TT>)</TT><CODE> </CODE><CODE>&lt;-</CODE><CODE> </CODE>vec<CODE>.</CODE><TT>(</TT>p<TT>)</TT><CODE> </CODE>;<CODE> </CODE>vec<CODE>.</CODE><TT>(</TT>p<TT>)</TT><CODE> </CODE><CODE>&lt;-</CODE><CODE> </CODE>aux<CODE> </CODE><CODE> </CODE>;;<BR><CODE>val permute_element : 'a array -&gt; int -&gt; int -&gt; unit = &lt;fun&gt;</CODE><BR>

</PRE>
<BR>
<BR>
The choice of a good pivot determines the efficiency of the algorithm,
but we will use the simplest possible choice here: return the index
of the first element of the (sub)vector. 


<PRE><BR># <B>let</B><CODE> </CODE>choose_pivot<CODE> </CODE>vec<CODE> </CODE>start<CODE> </CODE>finish<CODE> </CODE><CODE>=</CODE><CODE> </CODE>start<CODE> </CODE>;;<BR><CODE>val choose_pivot : 'a -&gt; 'b -&gt; 'c -&gt; 'b = &lt;fun&gt;</CODE><BR>

</PRE>
<BR>
<BR>
Let us write the algorithm that we would like to use to permute the
elements of the vector around the pivot. 
<OL type=1>
<LI>
 Place the pivot at the beginning of the vector to be
 permuted; 
<LI> Initialize <I>i</I> to the index of the second element of the vector; 

<LI> Initialize <I>j</I> to the index of the last element of the vector; 

<LI> If the element at index <I>j</I> is greater than the pivot, permute
 it with the element at index <I>i</I> and increment <I>i</I>; otherwise,
 decrement <I>j</I>;

<LI> While <I>i</I>&lt;<I>j</I>, repeat the previous operation; 

<LI> At this stage, every element with index &lt;<I>i</I> (or equivalently,
 <I>j</I>) is less than the pivot, and all others are greater; if the
 element with index <I>i</I> is less than the pivot, permute it with
 the pivot; otherwise, permute its predecessor with the pivot. 
</OL>
In implementing this algorithm, it is natural to adopt imperative
control structures.


<PRE><BR># <B>let</B><CODE> </CODE>permute_pivot<CODE> </CODE>vec<CODE> </CODE>start<CODE> </CODE>finish<CODE> </CODE>ind_pivot<CODE> </CODE><CODE>=</CODE><CODE> </CODE><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE>permute_element<CODE> </CODE>vec<CODE> </CODE>start<CODE> </CODE>ind_pivot<CODE> </CODE>;<BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><B>let</B><CODE> </CODE>i<CODE> </CODE><CODE>=</CODE><CODE> </CODE>ref<CODE> </CODE><TT>(</TT>start<CODE>+</CODE><CODE>1</CODE><TT>)</TT><CODE> </CODE><B>and</B><CODE> </CODE>j<CODE> </CODE><CODE>=</CODE><CODE> </CODE>ref<CODE> </CODE>finish<CODE> </CODE><B>and</B><CODE> </CODE>pivot<CODE> </CODE><CODE>=</CODE><CODE> </CODE>vec<CODE>.</CODE><TT>(</TT>start<TT>)</TT><CODE> </CODE><B>in</B><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><B>while</B><CODE> </CODE><CODE>!</CODE>i<CODE> </CODE><CODE>&lt;</CODE><CODE> </CODE><CODE>!</CODE>j<CODE> </CODE><B>do</B><CODE> </CODE><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><B>if</B><CODE> </CODE>vec<CODE>.</CODE><TT>(</TT><CODE>!</CODE>j<TT>)</TT><CODE> </CODE><CODE>&gt;=</CODE><CODE> </CODE>pivot<CODE> </CODE><B>then</B><CODE> </CODE>decr<CODE> </CODE>j<CODE> </CODE><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><B>else</B><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><B>begin</B><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE>permute_element<CODE> </CODE>vec<CODE> </CODE><CODE>!</CODE>i<CODE> </CODE><CODE>!</CODE>j<CODE> </CODE>;<BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE>incr<CODE> </CODE>i<BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><B>end</B><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><B>done</B><CODE> </CODE>;<BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><B>if</B><CODE> </CODE>vec<CODE>.</CODE><TT>(</TT><CODE>!</CODE>i<TT>)</TT><CODE> </CODE><CODE>&gt;</CODE><CODE> </CODE>pivot<CODE> </CODE><B>then</B><CODE> </CODE>decr<CODE> </CODE>i<CODE> </CODE>;<BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE>permute_element<CODE> </CODE>vec<CODE> </CODE>start<CODE> </CODE><CODE>!</CODE>i<CODE> </CODE>;<BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE>!</CODE>i<CODE> </CODE><BR><CODE> </CODE>;;<BR><CODE>val permute_pivot : 'a array -&gt; int -&gt; int -&gt; int -&gt; int = &lt;fun&gt;</CODE><BR>

</PRE>

In addition to its effects on the vector, this function returns the
index of the pivot as its result. <BR>
<BR>
All that remains is to put together the different stages and add the
recursion on the sub-vectors.


<PRE><BR># <B>let</B><CODE> </CODE><B>rec</B><CODE> </CODE>quick<CODE> </CODE>vec<CODE> </CODE>start<CODE> </CODE>finish<CODE> </CODE><CODE>=</CODE><CODE> </CODE><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><B>if</B><CODE> </CODE>start<CODE> </CODE><CODE>&lt;</CODE><CODE> </CODE>finish<CODE> </CODE><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><B>then</B><CODE> </CODE><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><B>let</B><CODE> </CODE>pivot<CODE> </CODE><CODE>=</CODE><CODE> </CODE>choose_pivot<CODE> </CODE>vec<CODE> </CODE>start<CODE> </CODE>finish<CODE> </CODE><B>in</B><CODE> </CODE><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><B>let</B><CODE> </CODE>place_pivot<CODE> </CODE><CODE>=</CODE><CODE> </CODE>permute_pivot<CODE> </CODE>vec<CODE> </CODE>start<CODE> </CODE>finish<CODE> </CODE>pivot<CODE> </CODE><B>in</B><CODE> </CODE><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE>quick<CODE> </CODE><TT>(</TT>quick<CODE> </CODE>vec<CODE> </CODE>start<CODE> </CODE><TT>(</TT>place_pivot<CODE>-</CODE><CODE>1</CODE><TT>)</TT><TT>)</TT><CODE> </CODE><TT>(</TT>place_pivot<CODE>+</CODE><CODE>1</CODE><TT>)</TT><CODE> </CODE>finish<BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><B>else</B><CODE> </CODE>vec<CODE> </CODE>;;<BR><CODE>val quick : 'a array -&gt; int -&gt; int -&gt; 'a array = &lt;fun&gt;</CODE><BR>

</PRE>
<BR>
<BR>
We have used the two styles here. The chosen pivot serves as argument
to the permutation around this pivot, and the index of the pivot after
the permutation is an argument to the recursive call. By contrast,
the vector obtained after the permutation is not returned by the
<TT>permute_pivot</TT> function; instead, this result is produced by
side effect. However, the <TT>quick</TT> function returns a vector,
and the sorting of sub-vectors is obtained by composition of recursive
calls. <BR>
<BR>
The main function is: 


<PRE><BR># <B>let</B><CODE> </CODE>quicksort<CODE> </CODE>vec<CODE> </CODE><CODE>=</CODE><CODE> </CODE>quick<CODE> </CODE>vec<CODE> </CODE><CODE>0</CODE><CODE> </CODE><TT>(</TT><TT>(</TT>Array.length<CODE> </CODE>vec<TT>)</TT><CODE>-</CODE><CODE>1</CODE><TT>)</TT><CODE> </CODE>;;<BR><CODE>val quicksort : 'a array -&gt; 'a array = &lt;fun&gt;</CODE><BR>

</PRE>

It is a polymorphic function because the order relation <TT>&lt;</TT> on
vector elements is itself polymorphic. 


<PRE><BR># <B>let</B><CODE> </CODE>t1<CODE> </CODE><CODE>=</CODE><CODE> </CODE><CODE>[|</CODE><CODE>4</CODE>;<CODE>8</CODE>;<CODE>1</CODE>;<CODE>1</CODE><CODE>2</CODE>;<CODE>7</CODE>;<CODE>3</CODE>;<CODE>1</CODE>;<CODE>9</CODE><CODE>|]</CODE><CODE> </CODE>;;<BR><CODE>val t1 : int array = [|4; 8; 1; 12; 7; 3; 1; 9|]</CODE><BR># quicksort<CODE> </CODE>t1<CODE> </CODE>;;<BR><CODE>- : int array = [|1; 1; 3; 4; 7; 8; 9; 12|]</CODE><BR># t1<CODE> </CODE>;;<BR><CODE>- : int array = [|1; 1; 3; 4; 7; 8; 9; 12|]</CODE><BR># <B>let</B><CODE> </CODE>t2<CODE> </CODE><CODE>=</CODE><CODE> </CODE><CODE>[|</CODE><CODE>"the"</CODE>;<CODE> </CODE><CODE>"little"</CODE>;<CODE> </CODE><CODE>"cat"</CODE>;<CODE> </CODE><CODE>"is"</CODE>;<CODE> </CODE><CODE>"dead"</CODE><CODE>|]</CODE><CODE> </CODE>;;<BR><CODE>val t2 : string array = [|"the"; "little"; "cat"; "is"; "dead"|]</CODE><BR># quicksort<CODE> </CODE>t2<CODE> </CODE>;;<BR><CODE>- : string array = [|"cat"; "dead"; "is"; "little"; "the"|]</CODE><BR># t2<CODE> </CODE>;;<BR><CODE>- : string array = [|"cat"; "dead"; "is"; "little"; "the"|]</CODE><BR>

</PRE>
<BR>
<BR>
<A NAME="toc49"></A>
<H3> Shared or Copy Values</H3>
<A NAME="subsec-partagevscopie"></A>
When the values that we manipulate are not mutable, it does not matter
whether they are shared or not. 


<PRE><BR># <B>let</B><CODE> </CODE>id<CODE> </CODE>x<CODE> </CODE><CODE>=</CODE><CODE> </CODE>x<CODE> </CODE>;;<BR><CODE>val id : 'a -&gt; 'a = &lt;fun&gt;</CODE><BR># <B>let</B><CODE> </CODE>a<CODE> </CODE><CODE>=</CODE><CODE> </CODE><CODE>[</CODE><CODE> </CODE><CODE>1</CODE>;<CODE> </CODE><CODE>2</CODE>;<CODE> </CODE><CODE>3</CODE><CODE> </CODE><CODE>]</CODE><CODE> </CODE>;;<BR><CODE>val a : int list = [1; 2; 3]</CODE><BR># <B>let</B><CODE> </CODE>b<CODE> </CODE><CODE>=</CODE><CODE> </CODE>id<CODE> </CODE>a<CODE> </CODE>;;<BR><CODE>val b : int list = [1; 2; 3]</CODE><BR>

</PRE>

Whether <TT>b</TT> is a copy of the list <TT>a</TT> or the very same
list makes no difference, because these are intangible values anyway.
But if we put modifiable values in place of integers, we need to know
whether modifying one value causes a change in the other. <BR>
<BR>
The implementation of polymorphism in Objective CAML causes immediate values
to be copied, and structured values to be shared. Even though
arguments are always passed by value, only the pointer to a structured
value is copied. This is the case even in the function <TT>id</TT>:


<PRE><BR># <B>let</B><CODE> </CODE>a<CODE> </CODE><CODE>=</CODE><CODE> </CODE><CODE>[|</CODE><CODE> </CODE><CODE>1</CODE><CODE> </CODE>;<CODE> </CODE><CODE>2</CODE><CODE> </CODE>;<CODE> </CODE><CODE>3</CODE><CODE> </CODE><CODE>|]</CODE><CODE> </CODE>;;<BR><CODE>val a : int array = [|1; 2; 3|]</CODE><BR># <B>let</B><CODE> </CODE>b<CODE> </CODE><CODE>=</CODE><CODE> </CODE>id<CODE> </CODE>a<CODE> </CODE>;;<BR><CODE>val b : int array = [|1; 2; 3|]</CODE><BR># a<CODE>.</CODE><TT>(</TT><CODE>1</CODE><TT>)</TT><CODE> </CODE><CODE>&lt;-</CODE><CODE> </CODE><CODE>4</CODE><CODE> </CODE>;;<BR><CODE>- : unit = ()</CODE><BR># a<CODE> </CODE>;;<BR><CODE>- : int array = [|1; 4; 3|]</CODE><BR># b<CODE> </CODE>;;<BR><CODE>- : int array = [|1; 4; 3|]</CODE><BR>

</PRE>

We have here a genuine programming choice to decide which is the most
efficient way to represent a data structure. On one hand, using
mutable values allows manipulations in place, which means without
allocation, but requires us to make copies sometimes when immutable
data would have allowed sharing. We illustrate this here with two
ways to implement lists. 


<PRE><BR># <B>type</B><CODE> </CODE><I>'a</I><CODE> </CODE>list_immutable<CODE> </CODE><CODE>=</CODE><CODE> </CODE>LInil<CODE> </CODE><CODE>|</CODE><CODE> </CODE>LIcons<CODE> </CODE><B>of</B><CODE> </CODE><I>'a</I><CODE> </CODE><CODE>*</CODE><CODE> </CODE><I>'a</I><CODE> </CODE>list_immutable<CODE> </CODE>;;<BR># <B>type</B><CODE> </CODE><I>'a</I><CODE> </CODE>list_mutable<CODE> </CODE><CODE>=</CODE><CODE> </CODE>LMnil<CODE> </CODE><CODE>|</CODE><CODE> </CODE>LMcons<CODE> </CODE><B>of</B><CODE> </CODE><I>'a</I><CODE> </CODE><CODE>*</CODE><CODE> </CODE><I>'a</I><CODE> </CODE>list_mutable<CODE> </CODE>ref<CODE> </CODE>;;<BR>

</PRE>
<BR>
<BR>
The immutable lists are strictly equivalent to lists built into
Objective CAML, while the mutable lists are closer to the style of C, in
which a cell is a value together with a reference to the following cell. <BR>
<BR>
With immutable lists, there is only one way to write concatenation,
and it requires duplicating the structure of the first list; by
contrast, the second list may be shared with the result. 


<PRE><BR># <B>let</B><CODE> </CODE><B>rec</B><CODE> </CODE>concat<CODE> </CODE>l1<CODE> </CODE>l2<CODE> </CODE><CODE>=</CODE><CODE> </CODE><B>match</B><CODE> </CODE>l1<CODE> </CODE><B>with</B><CODE> </CODE><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE>LInil<CODE> </CODE>-&gt;<CODE> </CODE>l2<BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE>|</CODE><CODE> </CODE>LIcons<CODE> </CODE><TT>(</TT>a<CODE>,</CODE>l11<TT>)</TT><CODE> </CODE>-&gt;<CODE> </CODE>LIcons<TT>(</TT>a<CODE>,</CODE><CODE> </CODE><TT>(</TT>concat<CODE> </CODE>l11<CODE> </CODE>l2<TT>)</TT><TT>)</TT><CODE> </CODE>;;<BR><CODE>val concat : 'a list_immutable -&gt; 'a list_immutable -&gt; 'a list_immutable =</CODE><BR><CODE>  &lt;fun&gt;</CODE><BR><BR># <B>let</B><CODE> </CODE>li1<CODE> </CODE><CODE>=</CODE><CODE> </CODE>LIcons<TT>(</TT><CODE>1</CODE><CODE>,</CODE><CODE> </CODE>LIcons<TT>(</TT><CODE>2</CODE><CODE>,</CODE><CODE> </CODE>LInil<TT>)</TT><TT>)</TT><BR><CODE> </CODE><B>and</B><CODE> </CODE>li2<CODE> </CODE><CODE>=</CODE><CODE> </CODE><CODE> </CODE>LIcons<TT>(</TT><CODE>3</CODE><CODE>,</CODE><CODE> </CODE>LIcons<TT>(</TT><CODE>4</CODE><CODE>,</CODE><CODE> </CODE>LInil<TT>)</TT><TT>)</TT><CODE> </CODE>;;<BR><CODE>val li1 : int list_immutable = LIcons (1, LIcons (2, LInil))</CODE><BR><CODE>val li2 : int list_immutable = LIcons (3, LIcons (4, LInil))</CODE><BR># <B>let</B><CODE> </CODE>li3<CODE> </CODE><CODE>=</CODE><CODE> </CODE>concat<CODE> </CODE>li1<CODE> </CODE>li2<CODE> </CODE>;;<BR><CODE>val li3 : int list_immutable =</CODE><BR><CODE>  LIcons (1, LIcons (2, LIcons (3, LIcons (4, LInil))))</CODE><BR># li1<CODE>==</CODE>li3<CODE> </CODE>;;<BR><CODE>- : bool = false</CODE><BR># <B>let</B><CODE> </CODE>tlLI<CODE> </CODE>l<CODE> </CODE><CODE>=</CODE><CODE> </CODE><CODE> </CODE><B>match</B><CODE> </CODE>l<CODE> </CODE><B>with</B><CODE> </CODE><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE>LInil<CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE>-&gt;<CODE> </CODE>failwith<CODE> </CODE><CODE>"Liste vide"</CODE><CODE> </CODE><BR><CODE> </CODE><CODE>|</CODE><CODE> </CODE>LIcons<TT>(</TT><CODE>_,</CODE>x<TT>)</TT><CODE> </CODE>-&gt;<CODE> </CODE>x<CODE> </CODE>;;<BR><CODE>val tlLI : 'a list_immutable -&gt; 'a list_immutable = &lt;fun&gt;</CODE><BR># tlLI<TT>(</TT>tlLI<TT>(</TT>li3<TT>)</TT><TT>)</TT><CODE> </CODE><CODE>==</CODE><CODE> </CODE>li2<CODE> </CODE>;;<BR><CODE>- : bool = true</CODE><BR>

</PRE>

From these examples, we see that the first cells of <TT>li1</TT> and
<TT>li3</TT> are distinct, while the second half of <TT>li3</TT> is
exactly <TT>li2</TT>.<BR>
<BR>
With mutable lists, we have a choice between modifying arguments
(function <TT>concat_share</TT>) and creating a new value (function
<TT>concat_copy</TT>). 


<PRE><BR># <B>let</B><CODE> </CODE><B>rec</B><CODE> </CODE>concat_copy<CODE> </CODE>l1<CODE> </CODE>l2<CODE> </CODE><CODE>=</CODE><CODE> </CODE><CODE> </CODE><B>match</B><CODE> </CODE>l1<CODE> </CODE><B>with</B><CODE> </CODE><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE>LMnil<CODE> </CODE><CODE> </CODE>-&gt;<CODE> </CODE>l2<BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE>|</CODE><CODE> </CODE>LMcons<CODE> </CODE><TT>(</TT>x<CODE>,</CODE>l11<TT>)</TT><CODE> </CODE>-&gt;<CODE> </CODE>LMcons<TT>(</TT>x<CODE>,</CODE><CODE> </CODE>ref<CODE> </CODE><TT>(</TT>concat_copy<CODE> </CODE><CODE>!</CODE>l11<CODE> </CODE>l2<TT>)</TT><TT>)</TT><CODE> </CODE>;;<BR><CODE>val concat_copy : 'a list_mutable -&gt; 'a list_mutable -&gt; 'a list_mutable =</CODE><BR><CODE>  &lt;fun&gt;</CODE><BR>

</PRE>

This first solution, <TT>concat_copy</TT>, gives a result similar to
the previous function, <TT>concat</TT>. A second solution shares its
arguments with its result fully: 


<PRE><BR># <B>let</B><CODE> </CODE>concat_share<CODE> </CODE>l1<CODE> </CODE>l2<CODE> </CODE><CODE>=</CODE><CODE> </CODE><BR><CODE> </CODE><CODE> </CODE><B>match</B><CODE> </CODE>l1<CODE> </CODE><B>with</B><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE>LMnil<CODE> </CODE>-&gt;<CODE> </CODE>l2<BR><CODE> </CODE><CODE> </CODE><CODE>|</CODE><CODE> </CODE><CODE>_</CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE>-&gt;<CODE> </CODE><B>let</B><CODE> </CODE><B>rec</B><CODE> </CODE>set_last<CODE> </CODE><CODE>=</CODE><CODE> </CODE><B>function</B><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE>LMnil<CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE>-&gt;<CODE> </CODE>failwith<CODE> </CODE><CODE>"concat_share : impossible case!!"</CODE><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE>|</CODE><CODE> </CODE>LMcons<TT>(</TT><CODE>_,</CODE>l<TT>)</TT><CODE> </CODE>-&gt;<CODE> </CODE><B>if</B><CODE> </CODE><CODE>!</CODE>l<CODE>=</CODE>LMnil<CODE> </CODE><B>then</B><CODE> </CODE>l<CODE>:=</CODE>l2<CODE> </CODE><B>else</B><CODE> </CODE>set_last<CODE> </CODE><CODE>!</CODE>l<BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><B>in</B><BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE>set_last<CODE> </CODE>l1<CODE> </CODE>;<BR><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE><CODE> </CODE>l1<CODE> </CODE>;;<BR><CODE>val concat_share : 'a list_mutable -&gt; 'a list_mutable -&gt; 'a list_mutable =</CODE><BR><CODE>  &lt;fun&gt;</CODE><BR>

</PRE>

Concatenation with sharing does not require any allocation, and
therefore does not use the constructor <TT>LMcons</TT>. Instead, it
suffices to cause the last cell of the first list to point to the
second list. However, this version of concatenation has the potential
weakness that it alters arguments passed to it.


<PRE><BR># <B>let</B><CODE> </CODE>lm1<CODE> </CODE><CODE>=</CODE><CODE> </CODE>LMcons<TT>(</TT><CODE>1</CODE><CODE>,</CODE><CODE> </CODE>ref<CODE> </CODE><TT>(</TT>LMcons<TT>(</TT><CODE>2</CODE><CODE>,</CODE><CODE> </CODE>ref<CODE> </CODE>LMnil<TT>)</TT><TT>)</TT><TT>)</TT><BR><CODE> </CODE><B>and</B><CODE> </CODE>lm2<CODE> </CODE><CODE>=</CODE><CODE> </CODE>LMcons<TT>(</TT><CODE>3</CODE><CODE>,</CODE><CODE> </CODE>ref<CODE> </CODE><TT>(</TT>LMcons<TT>(</TT><CODE>4</CODE><CODE>,</CODE><CODE> </CODE>ref<CODE> </CODE>LMnil<TT>)</TT><TT>)</TT><TT>)</TT><CODE> </CODE>;;<BR><CODE>val lm1 : int list_mutable =</CODE><BR><CODE>  LMcons (1, {contents=LMcons (2, {contents=LMnil})})</CODE><BR><CODE>val lm2 : int list_mutable =</CODE><BR><CODE>  LMcons (3, {contents=LMcons (4, {contents=LMnil})})</CODE><BR># <B>let</B><CODE> </CODE>lm3<CODE> </CODE><CODE>=</CODE><CODE> </CODE>concat_share<CODE> </CODE>lm1<CODE> </CODE>lm2<CODE> </CODE>;;<BR><CODE>val lm3 : int list_mutable =</CODE><BR><CODE>  LMcons (1, {contents=LMcons (2, {contents=LMcons (...)})})</CODE><BR>

</PRE>

We do indeed obtain the expected result for <TT>lm3</TT>. However,
the value bound to <TT>lm1</TT> has been modified. 


<PRE><BR># lm1<CODE> </CODE>;;<BR><CODE>- : int list_mutable =</CODE><BR><CODE>LMcons (1, {contents=LMcons (2, {contents=LMcons (...)})})</CODE><BR>

</PRE>

This may therefore have consequences on the rest of the program. <BR>
<BR>
<A NAME="toc50"></A>
<H3> How to Choose your Style</H3>In a purely functional program, side effects are forbidden, and this
excludes mutable data structures, exceptions, and input/output. We
prefer, though, a less restrictive definition of the functional style,
saying that functions that do not modify their global environment may
be used in a functional style. Such a function may manipulate mutable
values locally, and may therefore be written in an imperative style,
but must not modify global variables, nor its
arguments. We permit them to raise exceptions in addition. Viewed
from outside, these functions may be considered ``black boxes.''
Their behavior matches a function written in a purely functional
style, apart from being able of breaking control flow by raising an
exception. In the same spirit, a mutable value which can no longer be
modified after initialization may be used in a functional style. <BR>
<BR>
On the other hand, a program written in an imperative style still benefits
from the advantages provided by Objective CAML: static type safety,
automatic memory management, the exception mechanism, parametric
polymorphism, and type inference. <BR>
<BR>
The choice between the imperative and functional styles depends on the
application to be developed. We may nevertheless suggest some guidelines
based on the character of the application, and the criteria
considered important in the development process.
<UL>
<LI>
<B>choice of data structures</B>: The choice whether to use
mutable data structures follows from the style of programming
adopted. Indeed, the functional style is essentially incompatible
with modifying mutable values. By contrast, constructing and
traversing objects are the same whatever their status. This touches
the same issue as ``modification in place vs copying'' on
page&nbsp;<A HREF="book-ora038.html#subsec-partagevscopie">??</A>; we return to it again in
discussing criteria of efficiency. 

<LI><B>required data structures</B>: If a program must modify mutable data structures, then the imperative
style is the only one possible. If, on the other hand, 
you just have to traverse values, then adopting the functional
style guarantees the integrity of the data.<BR>Using recursive data structures requires the use of functions that are
themselves recursive. Recursive functions may be defined using either
of the two styles, but it is often easier to understand the creation
of a value following a recursive definition, which corresponds to a
functional approach, than to repeat the recursive processing on this
element. The functional style allows us to define generic iterators over the
structure of data, which factors out the work of development and makes
it faster. <BR>
<BR>

<LI> <B>criteria of efficiency</B>: Modification in place is far
more efficient than creating a value. When code efficiency is the
preponderant criterion, it will usually tip the balance in favor of
the imperative style. We note however that the need to avoid sharing
values may turn out to be a very hard task, and in the end costlier
than copying the values to begin with.<BR>Being purely functional has a cost. Partial application and using
functions passed as arguments from other functions has an execution
cost greater than total application of a function whose declaration is
visible. Using this eminently functional feature must thus be
avoided in those portions of a program where efficiency is crucial. <BR>
<BR>

<LI> <B>development criteria</B>: the higher level of abstraction of
functional programs permits them to be written more quickly, leading
to code that is more compact and contains fewer errors than the
equivalent imperative code, which is generally more verbose. The
functional style is better suited to the constraints imposed by
developing substantial applications. Since each function is not dependent upon its evaluation context,
functional can be easily divided into small units that can be examined
separately; as a consequence, the code is easier to read.<BR>Programs written using the functional style are more easily reusable
because of its better modularity, and because functions may be passed
as arguments to other functions. 
</UL>These remarks show that it is often a good idea to mix the two
programming styles within the same application. The functional
programming style is faster to develop and confers a simpler
organization to an application. However, portions whose execution
time is critical repay being developed in a more efficient imperative
style. <BR>
<BR>
<HR>
<A HREF="book-ora037.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora039.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
